package com.dxy.app.widget.shrink_upvote;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.dxy.app.R;
import java.util.ArrayList;
import java.util.List;

/**
  *Created by duxingyu on 2017/7/21.17:58
  *Email: 
  *Description:评论控件
  **/

public class VoteView extends LinearLayout {

  private List<ShrinkUpvoteView> list =new ArrayList<>();
  public VoteView(Context context) {
    super(context);
    initView();
  }

  public VoteView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    initView();
  }

  public VoteView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initView();
  }
  private void initView(){
    ViewGroup.LayoutParams lps =new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);
    setLayoutParams(lps);
    View.inflate(getContext(), R.layout.layout_vote_view,this);

    ShrinkUpvoteView dislike,just,like;

    dislike = (ShrinkUpvoteView) findViewById(R.id.suv_dislike);
    just = (ShrinkUpvoteView) findViewById(R.id.suv_just);
    like = (ShrinkUpvoteView) findViewById(R.id.suv_like);

    list.add(dislike);
    list.add(just);
    list.add(like);

    setListener(dislike,just,like);
  }
  private void setListener(ShrinkUpvoteView...views){
    for(ShrinkUpvoteView each:views){
      each.setListener((view, selected) -> notifyEmoj(view,selected));
    }
  }
  private void notifyEmoj(ShrinkUpvoteView view,boolean selected){
    for(ShrinkUpvoteView each:list){
      if(view !=each){
        each.setSelectState(selected);
      }
    }
    if(listener!=null){
      listener.curSelectVote(view.type);
    }
  }
  private VoteSelectListener listener;
  public void setListener(VoteSelectListener listener){
    this.listener =listener;
  }

}
